package com.eonmind.user.converts;

import com.alibaba.spring.util.ObjectUtils;
import com.eonmind.common.provider.entity.user.User;
import com.eonmind.common.provider.entity.user.UserLoginRecord;
import com.eonmind.user.dto.UserDto;
import com.eonmind.user.utils.MD5Utils;
import org.springframework.beans.BeanUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class UserConvert {
    /**
     * 将注册请求转换为用户对象
     *
     * @param userRegisterRequest 注册请求
     * @return 用户对象
     */
    public static User convertToUser(UserDto.UserRegisterRequest userRegisterRequest) {
        User user = new User();
        user.setUserName(userRegisterRequest.getUsername());
        user.setPassword(MD5Utils.encrypt(userRegisterRequest.getPassword()));
        user.setEmail(userRegisterRequest.getEmail());
        user.setPhoneNumber(userRegisterRequest.getPhoneNumber());
        user.setSex(userRegisterRequest.getSex());
        user.setPasswordComplexity(userRegisterRequest.getPasswordComplexity());
        return user;
    }

    public static Collection<UserDto.UserLoginRecordData> toUserLoginRecordDataList(List<UserLoginRecord> contentData) {
        return contentData.stream()
                .map(t -> {
                    UserDto.UserLoginRecordData userLoginRecordData = new UserDto.UserLoginRecordData();
                    BeanUtils.copyProperties(t, userLoginRecordData);
                    return userLoginRecordData;
                }).toList();
    }
}
